package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

// redis数据初始化监听器
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;



    // spring容器初始化之后, 马上执行的方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("==================vo数据初始化==start======================");
        // 从mysql中 查询所有攻略数据
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            if(strategyStatisRedisService.isExistVo(detail.getId())){
                continue;
            }
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(detail, vo);
            vo.setDestName(detail.getDest().getName());
            vo.setDestId(detail.getDest().getId());
            vo.setStrategyId(detail.getId());
            strategyStatisRedisService.setStrategyStarisVo(vo);
        }
        // 将攻略数据转换成vo对象即可
        System.out.println("==================vo数据初始化==end======================");

        System.out.println("==================zset数据初始化==start======================");
        // 热门的攻略排行初始化: 浏览数+回复数
        for (StrategyDetail detail : list) {
            int score = detail.getViewnum() + detail.getReplynum();
            // redis里面有的数据跳过
            if(strategyStatisRedisService.isValueInZset(detail.getId(), RedisKeys.STRATEGY_HOT_SCORE.getPrefix())){
                continue;
            }
            strategyStatisRedisService.addHotScore(detail.getId(), score);
        }
        // 国内外攻略排行初始化: 收藏 + 顶
        for (StrategyDetail detail : list) {
            int score = detail.getFavornum() + detail.getThumbsupnum();
            // redis里面有的数据跳过
            if(strategyStatisRedisService.isValueInZset(detail.getId(), RedisKeys.STRATEGY_ABROAD_SCORE.getPrefix()) || strategyStatisRedisService.isValueInZset(detail.getId(), RedisKeys.STRATEGY_CHINA_SCORE.getPrefix())){
                continue;
            }
            strategyStatisRedisService.addAbroadOrChina(detail.getId(), score);
        }



        System.out.println("==================zset数据初始化==end======================");
    }
}
